package com.jet.chat.service;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jet.chat.command.ChatCommand;
import com.jet.chat.exception.ChatExceptionCommandClassNotFound;
import com.jet.chat.exception.ChatExceptionCommandIllegalAccess;
import com.jet.chat.exception.ChatExceptionCommandInstantiation;
import com.jet.chat.server.ChatServer;
import com.jet.chat.server.ChatSession;


public class ChatMessageService {
	private static final ClassLoader classLoader = ChatMessageService.class.getClassLoader();
	
	public static void send(ChatMessage chatMessage, PrintWriter writer) {
		writer.println(chatMessage.getMessage());
		writer.flush();
	}
	public static void proceed(ChatMessage chatMessage) {
		
	}

	public synchronized static void proceed(ChatServer chatServer, ChatSession chatSession, ChatMessage chatMessage) throws ChatExceptionCommandClassNotFound, ChatExceptionCommandInstantiation, ChatExceptionCommandIllegalAccess {
		Pattern pattern = Pattern.compile("^/(\\w+).*$");
		Matcher matcher = pattern.matcher(chatMessage.getMessage());
		if (matcher.matches()) {
			
			String alias = matcher.group(1);
			String command = chatServer.getChatCommandAliases().get(alias);
			
			try {
				if (command == null) {
					ChatMessageService.send(new ChatMessage("no such command: '" + alias +"'"), chatSession.getWriter());
				} else {
					ChatCommand chatCommand = chatServer.getChatCommands().get(command);
					
					if (chatCommand == null) {
						Class<?> commandClass = classLoader.loadClass("ChatCommand" + command);
						chatCommand = (ChatCommand) commandClass.newInstance();
						chatServer.getChatCommands().put(command, chatCommand);
					}
					
					chatCommand.execute(chatServer, chatSession, chatMessage);
				}
			} catch (ClassNotFoundException e) {
				ChatMessageService.send(new ChatMessage("no such command: '" + alias +"'"), chatSession.getWriter());
				throw new ChatExceptionCommandClassNotFound("");
				//e.printStackTrace();
			} catch (InstantiationException e) {
				ChatMessageService.send(new ChatMessage("no such command: '" + alias +"'"), chatSession.getWriter());
				throw new ChatExceptionCommandInstantiation("");
				//e.printStackTrace();
			} catch (IllegalAccessException e) {
				ChatMessageService.send(new ChatMessage("no such command: '" + alias +"'"), chatSession.getWriter());
				throw new ChatExceptionCommandIllegalAccess("");
				//e.printStackTrace();
			}
		} else {
			if (chatMessage.getMessage() == null || chatMessage.getMessage().equals(null)) {
				
			} else {
				chatServer.getDbService().addHistory(chatSession.getId(), 0, chatSession.getChannelName(), chatMessage.getMessage());
				ChatMessage message = new ChatMessage(chatSession.getChannelName() + ":" + chatSession.getUsername() + " > " + chatMessage.getMessage());
				for (ChatSession session : chatServer.getChannel(chatSession.getChannelName()).getSessions()) {
					send(message, session.getWriter());
				}
			}
		}
	}
}
